package com.votereport.android;


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Properties;

import android.location.Location;
import android.util.Log;

import com.votereport.android.net.HttpManager;

/*
 * This class handles all of the REST calls into the back-end reporting system
 */
public class Reporter implements VoteReportConstants 
{
	private final static String TAG = "Reporter";
	
	private static String deviceGuid = null;
	
	/**
	 * @return the deviceGuid
	 */
	public static String getDeviceGuid() {
		return deviceGuid;
	}

	/**
	 * @param deviceGuid the deviceGuid to set
	 */
	public static void setDeviceGuid(String deviceGuid) {
		Reporter.deviceGuid = deviceGuid;
	}

	public static String getLocationName (Location location) throws Exception
	{
		// Setter for the user's location; initiate a HTTP request to get the name of this place
		// when the user's location is set
		
		String latlon = Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude());
		
		Properties params = new Properties();
		params.put("latlon", latlon);
		String locationName = HttpManager.doGet (TWITTERVISION_LOCATION_LOOKUP_URL, params);
		
		Log.i(TAG, "getLocationName resp: " + locationName);
		
		return locationName;
		
	}
	
	public static boolean submitReport (String reporterName, String pollingPlace, String locationName, Location location, String waitTime, String rating, String comment, String tags)
	{
		//39.024,-76.511:2000
		

		Log.i(TAG,"submitReport: submitting...");
		
		String url = VoteReportConstants.VOTEREPORT_REPORTS_URL;
		
		NumberFormat f = NumberFormat.getInstance();
		 if (f instanceof DecimalFormat) {
		     ((DecimalFormat) f).setMinimumFractionDigits(3);
		     ((DecimalFormat) f).setMaximumFractionDigits(3);
		     
		 }
		 

		String latlon = f.format(location.getLatitude()) + "," 
		+ f.format(location.getLongitude()) + ":"
		+ (int)(location.getAccuracy());
		
		Log.i(TAG,"latlon="+latlon);
		
		Properties props = new Properties();
		
		props.put("reporter[uniqueid]", deviceGuid);
		props.put("reporter[latlon]", latlon);
		props.put("reporter[profile_location]", locationName);
		props.put("reporter[name]", reporterName);
		
		props.put("polling_place[name]", pollingPlace);
		props.put("report[wait_time]", waitTime);
		props.put("report[text]", comment);
		props.put("report[rating]", rating);
		props.put("report[tag_string]", tags);
		
		String response = null;
		
		try
		{
			response = HttpManager.doPost (url, props);
			Log.i(TAG,"submitReport response: " + response);
		
		}
		catch (Exception e)
		{
			Log.i(TAG,"error doing post: " + e.toString());
		}
		
		
		if (response == null)
			return false;
		else
			return true;

	}
	
	public static void postAudioFile ()
	{
		/*
		 * HTTPManager *httpRequest = [[HTTPManager alloc] init];
	httpRequest.target = self;
	httpRequest.targetSelector = @selector(reportComplete:);
	NSString *soundfile = [params valueForKey:@"soundfile"];
	if (soundfile)
		[httpRequest uploadFile:soundfile toUrl:VOTEREPORT_REPORTS_URL withParameters:params];
	else
		[httpRequest performRequestWithMethod:@"POST" toUrl:VOTEREPORT_REPORTS_URL withParameters:params];

		 */
	}
	
	
	
	
}

